package edu.gdkm.demo8;

import java.io.*;

public class FileDemo7 {
    /*
    * 1、读取facebook.txt,给每个学生按顺序生成一个学号，学号格式：22060201xx
    * 2、将带有学号姓名的信息输出到facebook_number.txt,每个同学独占一行，输出格式：2206020101，张三
    * */
    public static void main(String[] args) throws IOException {
        File file = new File("./facebook.txt");
        FileReader fr = new FileReader(file);
        //使用缓存流实现文件的逐行读取
        BufferedReader br=new BufferedReader(fr);
        String name;

        int count=0;
        while((name =br.readLine())!=null){
            count++;
        }
        String students[] = new String[count];
        count=0;
        //缓存流的逐行读取会将读取指针下移，如果需要从头开始重新读取文件，需要重建缓存流
        br=new BufferedReader(new FileReader(file));
        while((name =br.readLine())!=null){
            students[count++]=name;
        }
        for(int i=0;i<students.length;i++){
            students[i]=((i+1)<10?"220602010"+(i+1):"22060201"+(i+1))+","+students[i];
        }

        File fileout=new File("./facebook_number.txt");
        FileWriter fw = new FileWriter(fileout);
        BufferedWriter bw=new BufferedWriter(fw);
        if(!fileout.exists()){
            fileout.createNewFile();
        }
        for(String student:students){
            bw.write(student);
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }
}
